module PrettifyTest(tests) where

import Test.HUnit
import Prettify

doc lines = fsep (map text lines)

docTest msg lines input = msg ~: doc lines @=? input

appendTests =
    TestList["two strings" ~: text "foobar" @=? text "foo" <> text "bar",
             "empty + empty" ~: empty @=? empty <> empty,
             "empty + string" ~: text "foo" @=? empty <> text "foo",
             "string + empty" ~: text "foo" @=? text "foo" <> empty,
             docTest "string + lines" ["12", "3", "4"]
                         (text "1" <> fsep [text "2", text "3", text "4"]),
             docTest "lines + string" ["1", "2", "34"]
                         (fsep [text "1", text "2", text "3"] <> text "4"),
             docTest "lines + lines" ["1", "23", "4"]
                         (fsep [text "1", text "2"] <>
                               fsep [text "3", text "4"])]

hcatTests =
    TestList["no strings" ~: empty @=? hcat [],
             "one string" ~: text "foo" @=? hcat [text "foo"],
             "many strings" ~: text "foobarbaz" @=?
                            hcat [text "foo", text "bar", text "baz"],
             docTest "string + lines" ["12", "3", "4"]
                         (hcat [text "1",
                                fsep [text "2", text "3", text "4"]]),
             docTest "lines + lines + lines" ["1", "23", "45", "6"]
                         (hcat [fsep [text "1", text "2"],
                                fsep [text "3", text "4"],
                                fsep [text "5", text "6"]]),
             docTest "empty in middle" ["1", "23", "4"]
                         (hcat [fsep [text "1", text "2"],
                                empty,
                                fsep [text "3", text "4"]])]

fsepTests =
    TestList["fsep isn't hcat" ~: False @=?
                                   (hcat[text "foo", text "bar"] ==
                                    fsep[text "foo", text "bar"]),
             "empty + empty" ~: empty @=? fsep [empty, empty],
             "empty + 1" ~: text "foo" @=? fsep [empty, text "foo"],
             docTest "empty + 3" ["foo", "bar", "baz"]
                         (fsep [empty, text "foo", text "bar", text "baz"]),
             docTest "1 + empty" ["foo"] (fsep [text "foo", empty]),
             docTest "empty in middle" ["foo", "bar"]
                         (fsep [text "foo", empty, text "bar"]),
             docTest "fsep of fseps" ["1", "2", "3"]
                         (fsep [text "1", fsep [text "2", text "3"]])]

compactTests =
    TestList["empty" ~: "" @=? compact empty,
             "one string" ~: "foo" @=? compact (text "foo"),
             "many strings" ~: "1\n2\n3" @=? compact (doc ["1", "2", "3"])]

prettyTests =
    TestList["empty" ~: "" @=? pretty 3 empty,
             "long string" ~: "foobar" @=? pretty 3 (text "foobar"),
             "two fits" ~: "foo bar" @=? pretty 7 (doc ["foo", "bar"]),
             "two don't fit" ~: "foo\nbar" @=? pretty 6 (doc ["foo", "bar"]),
             "three all fit" ~: "1 2 3" @=? pretty 5 (doc ["1", "2", "3"]),
             "three two fit" ~: "1 2\n3" @=? pretty 4 (doc ["1", "2", "3"]),
             "three one fits" ~: "1\n2\n3" @=? pretty 2 (doc ["1", "2", "3"]),
             "four two two" ~: "1 2\n3 4" @=?
                            pretty 3 (doc ["1", "2", "3", "4"])]

tests = TestList[appendTests, hcatTests, fsepTests, compactTests, prettyTests]
